package com.example.config.Utils;

import com.example.entity.MI_Table;
import com.example.mapper.MedicineInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 封装药品信息上传工具类
 *
 * @author: liu
 * 2022-03-30-17:09
 **/
@Service
public class MedInfoInsertUtil {

    @Autowired
    private MedicineInfoMapper medicineInfoMapper;

    public void addMedInfo(MI_Table medicineInfo){
        //库存为0的直接插入
        if (medicineInfo.getM_inventory() == 0) {
            System.out.println("medicineInfo.getM_inventory() == 0");
            System.out.println("medicineInfo.getM_userid()=="+medicineInfo.getM_userid());
            medicineInfo.setM_inventory(medicineInfo.getM_purchasenum());
            medicineInfoMapper.insert_MedicineInfo(medicineInfo.getM_name(), medicineInfo.getM_inventory(), medicineInfo.getM_unit(),
                    medicineInfo.getM_specification(),medicineInfo.getM_price(),medicineInfo.getM_amount(),medicineInfo.getM_purchasenum(),
                    medicineInfo.getM_manufacturer(), medicineInfo.getM_majorfunction(), medicineInfo.getM_adversereaction(),
                    medicineInfo.getM_taboos(), medicineInfo.getM_validuntil(), medicineInfo.getM_kinds(), medicineInfo.getM_batchnum()
                    , medicineInfo.getM_approvalnum(), medicineInfo.getM_otc(),medicineInfo.getM_userid());
        }
        else {
            System.out.println("medicineInfo.getM_inventory() !== 0");
            //不为0则判断库存中是否有批次号和有效期相同的，若有则更新库存数即可
            int num=0;
            //更新库存数量
            medicineInfo.setM_inventory(medicineInfo.getM_inventory()+medicineInfo.getM_purchasenum());
            for (int i = 0; i < medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).size(); i++) {
                System.out.println(Objects.equals(medicineInfo.getM_batchnum(),
                        medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).get(i).getM_batchnum()));
                System.out.println(medicineInfo.getM_validuntil().equals(medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).get(i).getM_validuntil()));
                System.out.println("medicineInfo.getM_batchnum(),get(i).getM_batchnum(),i==="+medicineInfo.getM_batchnum()+";;"+medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).get(i).getM_batchnum()+";;"+i);
                System.out.println("medicineInfo.getM_validuntil(),get(i).getM_validuntil(),i==="+medicineInfo.getM_validuntil()+";;"+medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).get(i).getM_validuntil()+";;"+i);

                System.out.println(".get(i).getM_amount()=="+medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name())
                        .get(i).getM_amount());
                System.out.println("get(i).getM_purchasenum()=="+medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).
                        get(i).getM_purchasenum()+";;;medicineInfo.getM_purchasenum()=="+medicineInfo.getM_purchasenum());
                if(medicineInfo.getM_batchnum().equals(
                        medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).get(i).getM_batchnum()) &&
                        medicineInfo.getM_validuntil().equals(
                                medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).get(i).getM_validuntil())){

                    ///更新新插入相同批次的总价和进购件数
                    medicineInfo.setM_amount(medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name())
                            .get(i).getM_amount()+medicineInfo.getM_amount());
                    medicineInfo.setM_purchasenum(medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).
                            get(i).getM_purchasenum()+medicineInfo.getM_purchasenum());

                    medicineInfoMapper.updateMedInfoById(medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name())
                                    .get(i).getM_id(), medicineInfo.getM_inventory(),medicineInfo.getM_amount()
                            , medicineInfo.getM_purchasenum());


                    System.out.println("get(i).getM_id():"+medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).get(i).getM_id());
                    System.out.println("medicineInfo.getM_name()"+medicineInfo.getM_name());
                    System.out.println("medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name())"+medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()));
                    num++;
                    break;
                }

                System.out.println("num == " + num);
                //满足条件就跳出循环
            }
            //判断是否找到满足上述条件
            if(num == medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).size()-1){
                System.out.println("num == medicineInfoMapper.queryBacthNumByName(medicineInfo.getM_name()).size()-1");
                medicineInfo.setM_inventory(medicineInfo.getM_purchasenum());//没有找到批次号和有效期相同的则直接插入并将进购件数直接给到库存
                medicineInfoMapper.insert_MedicineInfo(medicineInfo.getM_name(), medicineInfo.getM_inventory(), medicineInfo.getM_unit(),
                        medicineInfo.getM_specification(),medicineInfo.getM_price(),medicineInfo.getM_amount(),medicineInfo.getM_purchasenum(),
                        medicineInfo.getM_manufacturer(), medicineInfo.getM_majorfunction(), medicineInfo.getM_adversereaction(),
                        medicineInfo.getM_taboos(), medicineInfo.getM_validuntil(), medicineInfo.getM_kinds(), medicineInfo.getM_batchnum()
                        , medicineInfo.getM_approvalnum(), medicineInfo.getM_otc(),medicineInfo.getM_userid());
            }

        }
    }
}
